package com.helen.ui.recoveryclass;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.helen.adapter.ElectronicClassLeftAdapter;
import com.helen.adapter.ElectronicClassRightAdapter;
import com.helen.entity.RecoveryClassEntity;
import com.helen.shopping.R;
import com.helen.ui.BaseFragment;
import com.helen.utils.MyLog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2018/5/9.
 */

public class BaseElectronicClassFragment extends BaseFragment {
    @BindView(R.id.rv_classify_left)
    RecyclerView rvClassifyLeft;
    @BindView(R.id.rv_classify_right)
    RecyclerView rvClassifyRight;
    private int id;
    private List<RecoveryClassEntity> list = new ArrayList<>();
    private List<RecoveryClassEntity> listLeft = new ArrayList<>();
    private List<RecoveryClassEntity> listRight = new ArrayList<>();
    private ElectronicClassLeftAdapter electronicClassLeftAdapter;
    private ElectronicClassRightAdapter electronicClassRightAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_base_electronic_class;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        id = getArguments().getInt("id", 0);
        list = (List<RecoveryClassEntity>) getArguments().getSerializable("list");

        try {
            setLeftData();
        } catch (Exception e) {
            MyLog.e("yang", "左边数据e==" + e.toString());
        }


        try {
            sortRightData(0);
        } catch (Exception e) {
            MyLog.e("yang", "右边数据e==" + e.toString());
        }


        electronicClassLeftAdapter = new ElectronicClassLeftAdapter(R.layout.item_electronic_class_list_left, listLeft);
        rvClassifyLeft.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvClassifyLeft.setAdapter(electronicClassLeftAdapter);

        electronicClassRightAdapter = new ElectronicClassRightAdapter(R.layout.item_electronic_class_list_right, listRight);
        rvClassifyRight.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvClassifyRight.setAdapter(electronicClassRightAdapter);

        electronicClassLeftAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                electronicClassLeftAdapter.setSelectedPostion(position);
                electronicClassLeftAdapter.notifyDataSetChanged();

                sortRightData(position);
                electronicClassRightAdapter.notifyDataSetChanged();

            }
        });


        electronicClassRightAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RecoveryClassEntity recoveryClassEntity = (RecoveryClassEntity) electronicClassRightAdapter.getItem(position);
                List<RecoveryClassEntity> selectedDataList = new ArrayList<>();
                selectedDataList.add(recoveryClassEntity);
                Intent ti = new Intent();
                ti.putExtra("selectedDataList", (Serializable) selectedDataList);
//                ti.putExtra("recoveryClassEntity", recoveryClassEntity);
                getActivity().setResult(getActivity().RESULT_OK, ti);
                getActivity().finish();
            }
        });
    }

    @Override
    protected void updateViews() {

    }

    public static Fragment myFragment(int id, List<RecoveryClassEntity> list) {
        BaseElectronicClassFragment fragment = new BaseElectronicClassFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putSerializable("list", (Serializable) list);
        fragment.setArguments(bundle);
        return fragment;
    }


    private void setLeftData() {
        for (int i = 0; i < list.size(); i++) {
            if (id == list.get(i).getParentid()) {
                listLeft.add(list.get(i));
                MyLog.e("yang", "左边数据==" + list.get(i).getName());
            }
        }
    }


    private void sortRightData(int position) {
        listRight.clear();
        for (int j = 0; j < list.size(); j++) {
            if (listLeft.get(position).getId() == list.get(j).getParentid()) {
                listRight.add(list.get(j));
                MyLog.e("yang", "右边数据==" + list.get(j).getName());
            }

        }
    }


}
